//
//  HLMatchFateView.m
//  YueYue
//
//  Created by hulu001 on 2023/6/5.
//

#import "HLMatchFateView.h"
#import "HLMatchUserCell.h"
#import "UIScrollView+ScrollAnimation.h"
@interface HLMatchFateView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UILabel *lineLabel;
@property (nonatomic, strong) NSArray <HLHomeListModel*>*users;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, assign) NSInteger totalItemCount;
@property (nonatomic, assign) BOOL isLeft;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation HLMatchFateView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _flowLayout.minimumLineSpacing = 14;
    _flowLayout.minimumInteritemSpacing = 14;
    
    CGFloat itemW = (SCREEN_WIDTH)/ 2.2;
    CGFloat itemH = itemW * 1.6;
    _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _flowLayout.itemSize = CGSizeMake(itemW, itemH);
        //设置代理，实现代理方法
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
    _collectionView.pagingEnabled = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = UIColorClear;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[HLMatchUserCell class] forCellWithReuseIdentifier:[HLMatchUserCell identifyCell]];
    [self addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
        make.height.mas_equalTo(itemH + 14);
    }];
    
    
    self.lineLabel = [[UILabel alloc] init];
    _lineLabel.font = UIFontBoldMake(20);
    _lineLabel.textColor = HLMainColor;
    _lineLabel.text = @"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -";
    [self addSubview:_lineLabel];
    [_lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(-40);
        make.right.mas_equalTo(40);
    }];
}

- (void)invalidateTimer
{
    [_timer invalidate];
    _timer = nil;
}


- (void)automaticScroll
{
    int currentIndex = [self currentIndex];
    int targetIndex = currentIndex + 1;
    
    if (targetIndex >= _totalItemCount) {
        targetIndex = _totalItemCount * 0.5;
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
        return;
    }
    
    CGFloat y = _collectionView.contentOffset.y;
    __block CGFloat x = _collectionView.contentOffset.x;
    [UIView animateWithDuration:0.1 animations:^{
        if (self.isLeft){
            x -= 5;
        } else {
            x += 5;
        }
        
        self.collectionView.contentOffset = CGPointMake(x, y);
    }];
}

- (int)currentIndex
{
    CGFloat itemW = SCREEN_WIDTH / 2.2;
    
    int index = (_collectionView.contentOffset.x + itemW * 0.5) / itemW;
    
    return MAX(0, index);
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.totalItemCount;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HLMatchUserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HLMatchUserCell identifyCell] forIndexPath:indexPath];
    long itemIndex = indexPath.item % _users.count;
    cell.model = self.users[itemIndex];
    return cell;
}

- (void)setModel:(HLFateModel *)model
{
    self.users = model.users;
    self.isLeft = model.index % 2;
    self.totalItemCount = self.users.count * 200;
    [self.collectionView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self animateWithCollection];
    });
    
}

- (void)animateWithCollection
{
    CGFloat itemW = SCREEN_WIDTH / 2.2 + 14;
    NSInteger animateCount = (self.totalItemCount - 3);
    CAMediaTimingFunction *timing = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    CGFloat animateTo = animateCount * itemW;
    if (self.isLeft){
        [self.collectionView setContentOffset:CGPointZero];
        [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x + animateTo, self.collectionView.contentOffset.y) withTimingFunction:timing duration:(animateCount * 2.2)];
    } else {
        [self.collectionView setContentOffset:CGPointMake(animateTo, 0)];
        [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x - animateTo, self.collectionView.contentOffset.y) withTimingFunction:timing duration:(animateCount * 2.2)];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self animateWithCollection];
}





@end
